package com.example.hp.qingyi.company.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.hp.qingyi.R;
import com.example.hp.qingyi.base.BaseFragment;
import com.example.hp.qingyi.company.fragment.adapter.FragmentViewPagerAdapter;
import com.example.hp.qingyi.company.fragment.fragment.OneFragment;
import com.example.hp.qingyi.company.fragment.fragment.ThreeFragment;
import com.example.hp.qingyi.company.fragment.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

import static com.example.hp.qingyi.app.MyApplication.getContext;

/**
 * 驿友页面
 */
public class CompanyFragment extends BaseFragment {

    private TabLayout tab;
    private ViewPager vp;
    private String tabTitle[] = {"结伴广场", "发现好友","附近驿友"};
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentManager supportFragmentManager;

    @Override
    public View initView() {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.company_fragment,null);
        initTablayout();
        return view;

    }

    private void initTablayout() {
        OneFragment oneFragment =  new OneFragment();
        TwoFragment twoFragment = new TwoFragment();
        ThreeFragment threeFragment = new ThreeFragment();
        fragments.add(oneFragment);
        fragments.add(twoFragment);
        fragments.add(threeFragment);
        vp.setAdapter(new FragmentViewPagerAdapter(getSupportFragmentManager(), fragments));

        tab.setupWithViewPager(vp);
        vp.setOffscreenPageLimit(3);
        for (int i = 0; i < tabTitle.length; i++) {
            tab.getTabAt(i).setText(tabTitle[i]);
        }
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        Log.i("initData","驿友");

    }

    public FragmentManager getSupportFragmentManager() {
        return supportFragmentManager;
    }
}
